package com.thinkgem.jeesite.common.job;

import java.util.List;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.thinkgem.jeesite.common.utils.SpringContextHolder;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.guobo.dao.AssetDao;
import com.thinkgem.jeesite.modules.guobo.dao.ExhHallDao;
import com.thinkgem.jeesite.modules.guobo.dao.NCAssetDao;
import com.thinkgem.jeesite.modules.guobo.dao.RwDao;
import com.thinkgem.jeesite.modules.guobo.dao.TrailLogDao;
import com.thinkgem.jeesite.modules.guobo.entity.Asset;
import com.thinkgem.jeesite.modules.guobo.entity.ExhHall;
import com.thinkgem.jeesite.modules.guobo.entity.Rw;
import com.thinkgem.jeesite.modules.guobo.entity.TrailLog;
import com.thinkgem.jeesite.modules.guobo.entity.WarningLog;
import com.thinkgem.jeesite.modules.sys.dao.UserDao;
import com.thinkgem.jeesite.modules.sys.entity.User;

/**
 * 模拟读写器发送读写记录到系统
 * 
 * @author wudavid
 *
 */
@Component
public class RwSendJob {
	private Logger logger = LoggerFactory.getLogger(getClass());

	TrailLogDao trailLogDao = SpringContextHolder.getBean(TrailLogDao.class);

	AssetDao assetDao = SpringContextHolder.getBean(AssetDao.class);

	RwDao rwDao = SpringContextHolder.getBean(RwDao.class);

	ExhHallDao exhHallDao = SpringContextHolder.getBean(ExhHallDao.class);
	
	UserDao userDao = SpringContextHolder.getBean(UserDao.class);
	
	NCAssetDao ncAssetDao = SpringContextHolder.getBean(NCAssetDao.class);
	
	//@Scheduled(cron = "*/30 * * * * ?")
	/*public void processSend() {
		logger.debug("模拟读写器发送预警记录开始执行!");
		try {
			WarningLog warningLog = new WarningLog();
			List<Asset> assets = assetDao.findList(new Asset());
			Random r = new Random();
			int index = r.nextInt(assets.size());
			// 取随机资产
			Asset as = assets.get(index);
			as.getExhHall();
			TrailLog trailLog = new TrailLog();
			trailLog.setRfid(as.getRfid());
			List<TrailLog> traillogs = trailLogDao.findListByRfid(trailLog);
			if (traillogs != null && traillogs.size() > 0) {
				trailLog = traillogs.get(0);
			}
			// 如果轨迹的最后一条记录为进入类型,则下条记录一定为出去类型,且展馆为最后记录的展馆
			if ((trailLog != null && StringUtils.isNotBlank(trailLog.getId()) && trailLog.getMotionType().equals("in"))
					|| traillogs.size() <= 0) {
				TrailLog temp = new TrailLog();
				temp.setAsset(as);
				temp.setMotionType("out");
				if(traillogs.size()>0){
					temp.setRw(trailLog.getRw());
					temp.preInsert();
					User user = new User();
					user.setLoginName("admin");
					user = userDao.getByLoginName(user);
					temp.setCreateBy(user);
					temp.setUpdateBy(user);
					trailLogDao.insert(temp);
				}else{
					List<Rw> rws = rwDao.findListByExhallId(as.getExhHall().getId());
					if (rws != null && rws.size() > 0) {
						int indexRw = r.nextInt(rws.size());
						Rw rw = rws.get(indexRw);
						temp.setRw(rw);
						temp.preInsert();
						User user = new User();
						user.setLoginName("admin");
						user = userDao.getByLoginName(user);
						temp.setCreateBy(user);
						temp.setUpdateBy(user);
						trailLogDao.insert(temp);
					}
				}				
			}
			// 轨迹最后记录我带出时
			if ((trailLog != null && StringUtils.isNotBlank(trailLog.getId()) && trailLog.getMotionType().equals("out"))
					&& traillogs.size() > 0) {
				List<ExhHall> exhHalls = exhHallDao.findList(new ExhHall());
				int indexExhall = r.nextInt(exhHalls.size());
				ExhHall exhHall = exhHalls.get(indexExhall);
				List<Rw> rws = rwDao.findListByExhallId(exhHall.getId());
				if (rws != null && rws.size() > 0) {
					int indexRw = r.nextInt(rws.size());
					Rw rw = rws.get(indexRw);
					TrailLog temp = new TrailLog();
					temp.setAsset(as);
					temp.setMotionType("in");
					temp.setRw(rw);
					temp.preInsert();
					User user = new User();
					user.setLoginName("admin");
					user = userDao.getByLoginName(user);
					temp.setCreateBy(user);
					temp.setUpdateBy(user);
					trailLogDao.insert(temp);
					as.setExhHall(exhHall);
					assetDao.update(as);
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		List<NCAsset> list = ncAssetDao.findPageNCAssets(1, 20);
		for (NCAsset ncAsset : list) {
			System.out.println(ncAsset.getName());
		}
	}*/
}
